package com.fanghaoran.dao;

import com.fanghaoran.bean.Interaction;
import com.fanghaoran.entity.Sign;
import com.fanghaoran.view.user.studyPlanFrame.StudyPlanPanel;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: win11
 * @Date: 2023/03/23/13:12
 * @Description: 打卡操作的数据库操作
 */
public class SignDao {
    /**
     *返回用户签到数据
     */
    public Interaction selectSignInfo() throws Exception {
        String sql="select * from sign_info where userId=?;";
        List<Sign> signs = BaseDao.executeQuery(Sign.class, sql, StudyPlanPanel.user.getId());
        if(signs.isEmpty()){
            return new Interaction(null,null);
        }
        return new Interaction(null,signs.get(0));
    }

    /**
     *更新用户签到数据
     */
    public void updateSignInfo(Sign sign,Boolean isConstant) throws SQLException {
        String sql="update sign_info set clockDays=?,flag=?,lastClockDate=? where userId=?;";
        //获取当前时间
        LocalDateTime date=LocalDateTime.now();
        //与上次签到时间判断是否连续签到
        if(isConstant){
            //连续签到就自增
            BaseDao.executeUpdate(sql, sign.getClockDays()+1,1,date,sign.getUserId());
        }else{
            //不是连续签到 连续签到天数就设为1
            BaseDao.executeUpdate(sql,1,1,date,sign.getUserId());
        }

    }
    /**
     * 更新flag标志
     */
    public void updateFlag(Sign sign) throws SQLException {
        String sql="update sign_info set flag=0 where userId=?;";
        try {
            BaseDao.executeUpdate(sql, sign.getUserId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
